package top.kent90.common.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {

	private static DateFormat sysDate = new SimpleDateFormat("yyyy-MM-dd");
	private static DateFormat sysMonth = new SimpleDateFormat("yyyy-MM");
	private static DateFormat sysDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 鑾峰彇褰撳墠绯荤粺鐨勬棩鏈�
	 * @return
	 */
	public static String getSystemDate() {
		return sysDate.format(new Date());
	}
	
	/**
	 * 鑾峰彇绯荤粺褰撳墠鏃堕棿
	 * @return
	 */
	public static String getSystemDateTime() {
		return sysDateTime.format(new Date());
	}
	
	//鑾峰彇绯荤粺鐨勫綋鍓嶆椂闂�
	public static String currentTime() {
		Date today = new Date();
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		return dateformat.format(today);

	}
	
	/**
	 * 鏍规嵁鏍煎紡鑾峰彇绯荤粺褰撳墠鏃堕棿
	 * @param format
	 * @return
	 */
	public static String getSystemDateTimeByFormat(String format){
		return (new SimpleDateFormat(format)).format(new Date());
	}
	
	/**
	 * 鏍规嵁鏍煎紡鑾峰彇缁欏畾鏃堕棿鐨勫瓧绗︿覆褰㈠紡
	 * @return
	 */
	public static String getSystemDateTime(Date date) {
		return sysDateTime.format(date);
	}

	/**
	 * 鑾峰彇褰撳墠鏈堝垎绗竴澶╃殑鏃ユ湡
	 * @return
	 */	
	public static String getFirstDayOfMonth() {
		Calendar c = Calendar.getInstance();
		c.set(Calendar.DATE, 1);
		return sysDate.format(c.getTime());
	}
	
	/**
	 * 鑾峰彇褰撳墠鏈堝垎鏈�鍚庝竴澶╃殑鏃ユ湡
	 * @return 褰撴湀鏈�鍚庝竴澶╁瓧绗︿覆
	 */
	public static String getLastDayOfMonth() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, 1);
		c.set(Calendar.DATE, 1);
		c.add(Calendar.DATE, -1);
		return sysDate.format(c.getTime());
	}
	
	/**
	 *  鑾峰彇褰撳墠鏃ユ湡鐨勫墠鍚嶯澶╂棩鏈�
	 * @param n 
	 * @return 濡傛灉n鏄ぇ浜�0鐨勬暣鏁帮紝鍒欒繑鍥炲綋鍓嶆棩鏈熷線鍚庣n澶╃殑鏃ユ湡
	 * 		         濡傛灉n鏄皬浜�0鐨勬暣鏁帮紝鍒欒繑鍥炲綋鍓嶆棩鏈熷線鍓嶇n澶╃殑鏃ユ湡
	 */
	public static String getSpecialDate(int n) {
		Calendar c = Calendar.getInstance();
		c.setTime(new Date());
		c.add(Calendar.DATE, n);
		return sysDate.format( c.getTime());
	}
	
	/**
	 * 鑾峰彇绯荤粺褰撳墠鏈堜唤涓婁竴涓湀鐨勬棩鏈�
	 * @return 杩斿洖鏍煎紡"yyyy-MM"
	 */
	public static String getLastMonth() {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		return sysMonth.format(c.getTime());
	}

	/**
	 * 鑾峰彇绯荤粺褰撳墠鏈堜唤
	 * @return 杩斿洖鏍煎紡"yyyy-MM"
	 */
	public static String getYearMonth() {
		Calendar c = Calendar.getInstance();
		return sysMonth.format(c.getTime());
	}

	/**
	 * 鑾峰彇褰撳墠骞翠唤
	 * @return
	 */
	public static String getYear() {
		Calendar c = Calendar.getInstance();
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		return format.format(c.getTime());
	}
	
	/**
	 * 鑾峰彇褰撳墠瀛ｅ害
	 * @return 1锛氫竴瀛ｅ害 锛� 2锛氫簩瀛ｅ害锛�3锛氫笁瀛ｅ害锛�4锛氬洓瀛ｅ害
	 */
	public static int getNowJd() {
		Calendar now = Calendar.getInstance();
		int month = now.get(Calendar.MONTH) + 1;
		if (month == 1 || month == 2 || month == 3) {
			return 1;
		} else if (month == 4 || month == 5 || month == 6) {
			return 2;
		} else if (month == 7 || month == 8 || month == 9) {
			return 3;
		} else if (month == 10 || month == 11 || month == 12) {
			return 4;
		}
		return 0;
	}
	
	/**
	 * 璇ユ柟娉曠敤鏉ヨ绠椾袱涓椂闂寸浉宸嚑澶�
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static long getDateTimeDayDiff(String time1, String time2) {
		long tianshu = 0;		
		try {
			// 閫氳繃DateFormat灏嗗瓧绗︿覆鏃堕棿杞崲鎴怐ate绫诲瀷鐨勬棩鏈熸椂闂�
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

			Date dateCreate = df.parse(time1);
			Date dateOver = df.parse(time2);
			// 灏咲ate鏍煎紡鐨勬椂闂磋缃负Calendar鏍煎紡鐨勬棩鍘嗘牸寮忥紝骞朵娇鐢–alendar绫讳腑鐨刧etTimeInMillis()鏂规硶寰楀埌瀵瑰簲鐨刲ong绫诲瀷
			Calendar cl = Calendar.getInstance();
			cl.setTime(dateCreate);
			long timeCreate = cl.getTimeInMillis();
			cl.setTime(dateOver);
			long timeOver = cl.getTimeInMillis();
			// 璁＄畻鐩稿樊澶氬皯澶�
			tianshu = (timeOver - timeCreate) / 1000 / 3600 / 24;

		} catch (ParseException e) {
			e.printStackTrace();
		}
		return tianshu;
	}
	
	
	/**
	 * 涓や釜鏃堕棿鐩稿樊绉掓暟
	 * @param time1
	 * @param time2
	 * @return
	 */
	public static long secondDiff(String time1, String time2) {
		long miaoshu = 0;
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
			Date dateCreate = df.parse(time1);
			Date dateOver = df.parse(time2);
			Calendar cl = Calendar.getInstance();
			cl.setTime(dateCreate);
			long timeCreate = cl.getTimeInMillis();
			cl.setTime(dateOver);
			long timeOver = cl.getTimeInMillis();
			miaoshu = (timeOver - timeCreate) / 1000;
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return miaoshu;
	}

	/**
	 * 褰撳墠骞翠唤+-鎿嶄綔
	 * @param i
	 * @return 濡傛灉num>0,鍒欒繑鍥瀗骞村悗鐨勫勾浠斤紝濡傛灉num<0鍒欒繑鍥瀗骞村墠鐨勫勾浠�
	 */
	public static String getYear(int num) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.YEAR, num);
		SimpleDateFormat format = new SimpleDateFormat("yyyy");
		return format.format(c.getTime());
	}
	
	/**
	 * 涓や釜鏃堕棿鍋氭瘮杈冿紝璁＄畻鐢熸晥鏃堕棿鑼冨洿
	 * @param num
	 * @param type
	 * @param date
	 * @return
	 */
	public static String getNextTimeStr(double num, int type, Date date) {
		long nowLong = date.getTime();
		Date time = new Date(nowLong + formatToTimeMillis(num, type));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sdf.format(time);
		return str;
	}	
	
	public static long formatToTimeMillis(double num, int type) {
		if (num <= 0)
			return 0;
		switch (type) {
		case 1:
			return (long) (num * 1000);
		case 2:
			return (long) (num * 60 * 1000);
		case 3:
			return (long) (num * 60 * 60 * 1000);
		case 4:
			return (long) (num * 24 * 60 * 60 * 1000);
		default:
			return -1;
		}
	}

	/**
	 * 璁＄畻涓や釜鏃ユ湡鐩稿樊鐨勫ぉ銆佹椂銆佸垎
	 * @param endDate
	 * @param nowDate
	 * @return
	 */
	public static String getDatePoor(Date endDate, Date nowDate) {

		long nd = 1000 * 24 * 60 * 60;
		long nh = 1000 * 60 * 60;
		long nm = 1000 * 60;
		// long ns = 1000;
		// 鑾峰緱涓や釜鏃堕棿鐨勬绉掓椂闂村樊寮�
		long diff = endDate.getTime() - nowDate.getTime();
		// 璁＄畻宸灏戝ぉ
		long day = diff / nd;
		// 璁＄畻宸灏戝皬鏃�
		long hour = diff % nd / nh;
		// 璁＄畻宸灏戝垎閽�
		long min = diff % nd % nh / nm;
		// 璁＄畻宸灏戠//杈撳嚭缁撴灉
		// long sec = diff % nd % nh % nm / ns;
		return day + "澶�" + hour + "灏忔椂" + min + "鍒嗛挓";
	}
	/**
	 * 鏌ヨ褰撳墠鏃堕棿鍑犲垎閽熷墠鍚庢椂闂�
	 *   x  鍑犲垎閽熷悗
	 *   -x  鍑犲垎閽熷墠
	 * @param 鏃堕棿鏍煎紡 yyyy-MM-dd HH:mm:ss
	 * @return
	 */
	public static String getnumMonth(int num){
		Calendar calendar = Calendar.getInstance();
	    calendar.add(Calendar.MINUTE, num);
		return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
	
	}
}
